import fs from "node:fs";
import yaml from "js-yaml";
import nodemail from "nodemailer";

class Mail {
  constructor() {
    this.mailInfo = yaml.load(fs.readFileSync("./src/config/password.yaml", "utf8"));
    this.transport = nodemail.createTransport({
      service: "qq", // 邮箱服务商
      host: "smtp.qq.com", // 邮箱服务器地址
      port: 465, // 端口号
      secure: true, // 安全连接
      auth: {
        user: this.mailInfo.user, // 邮箱账号
        pass: this.mailInfo.pass, // 授权码，不是邮箱密码
      },
    });
  }

  sendMail(options) {
    options.from = options.from || this.mailInfo.user;
    return this.transport.sendMail(options);
  }
}

export default new Mail();
